//
//  WcshV.swift
//  Auscultation
//
//  Created by 来 on 2020/12/21.
//

import Foundation

class WcshV: UIView {
    
    var bgView: UIView!
    
    var select: UIImageView!
    
    var callBack: ((WcshV?, String, Bool) -> Void)!
    
    func showIn(_ SV: UIView, _ callBack: @escaping (WcshV?, String, Bool) -> Void) -> Void {
        self.clipsToBounds = true
        self.callBack = callBack
        SV.addSubview(self)
        lgf_FillSuperview()
        
        config()
        
        bgView.transform = CGAffineTransform.init(scaleX: 0.8, y: 0.8)
        DispatchQueue.main.async {
            UIView.animate(withDuration: 0.3) {
                self.bgView.transform = CGAffineTransform.identity
                self.backgroundColor = UIColor.black.withAlphaComponent(0.2)
            }
        }
    }
    
    func config() {
        bgView = UIView.init()
        bgView.backgroundColor = UIColor.white
        bgView.clipsToBounds = true
        bgView.layer.cornerRadius = 5
        addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.equalTo(250)
            make.height.equalTo(170)
        }
        
        let label1 = UILabel.init()
        label1.text = "完成审核"
        label1.font = UIFont.boldSystemFont(ofSize: 16)
        label1.textColor = UIColor.init(lgf_HexString: "222222")
        label1.textAlignment = .center
        bgView.addSubview(label1)
        label1.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(45)
        }
        
        let line1 = UIView.init()
        line1.backgroundColor = UIColor.init(lgf_HexString: "f0f0f0")
        bgView.addSubview(line1)
        line1.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-45)
            make.left.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
        
        let line2 = UIView.init()
        line2.backgroundColor = UIColor.init(lgf_HexString: "f0f0f0")
        bgView.addSubview(line2)
        line2.snp.makeConstraints { (make) in
            make.top.equalTo(line1.snp.bottom)
            make.bottom.equalToSuperview()
            make.centerX.equalToSuperview()
            make.width.equalTo(0.5)
        }
        
        let qx = UILabel.init()
        qx.lgf_AddTap(target: self, action: #selector(WcshV.s_qx))
        qx.text = "取消"
        qx.textAlignment = .center
        qx.textColor = UIColor.init(lgf_HexString: "8D969D")
        bgView.addSubview(qx)
        qx.snp.makeConstraints { (make) in
            make.top.equalTo(line1.snp.bottom)
            make.left.bottom.equalToSuperview()
            make.right.equalTo(line2.snp.left)
        }
        
        let qd = UILabel.init()
        qd.lgf_AddTap(target: self, action: #selector(WcshV.s_qd))
        qd.text = "确定"
        qd.textAlignment = .center
        qd.textColor = UIColor.init(lgf_HexString: "0F88F2")
        bgView.addSubview(qd)
        qd.snp.makeConstraints { (make) in
            make.top.equalTo(line1.snp.bottom)
            make.right.bottom.equalToSuperview()
            make.left.equalTo(line2.snp.right)
        }
        
        let center = UIView.init()
        center.lgf_AddTap(target: self, action: #selector(s_center))
        bgView.addSubview(center)
        center.snp.makeConstraints { (make) in
            make.top.equalTo(label1.snp.bottom)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(line1.snp.top)
        }
        
        let label2 = UILabel.init()
        label2.text = "是否需要仲裁"
        label2.textColor = UIColor.init(lgf_HexString: "222222")
        label2.font = UIFont.systemFont(ofSize: 15)
        center.addSubview(label2)
        label2.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview().offset(-5)
            make.centerX.equalToSuperview().offset(-20)
        }
        
        select = UIImageView.init(image: #imageLiteral(resourceName: "off"))
        select.lgf_ViewName = "unselect"
        center.addSubview(select)
        select.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview().offset(-5)
            make.centerX.equalToSuperview().offset(50)
            make.width.equalTo(32)
            make.height.equalTo(16)
        }
        
    }
    
    @objc func s_qx() {
        remove()
    }
    
    @objc func s_qd() {
        remove()
        callBack?(self, self.lgf_ViewName ?? "", select.lgf_ViewName == "select")
    }
    
    @objc func s_center() {
        if select.lgf_ViewName == "select" {
            select.image = #imageLiteral(resourceName: "off")
            select.lgf_ViewName = "unselect"
        } else {
            select.image = #imageLiteral(resourceName: "on")
            select.lgf_ViewName = "select"
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if touches.first?.view == self {
            remove()
        }
    }
    
    func remove() -> Void {
        UIView.animate(withDuration: 0.3, animations: {
            self.bgView.transform = CGAffineTransform.init(scaleX: 0.8, y: 0.8)
            self.bgView.alpha = 0.0
            self.backgroundColor = UIColor.black.withAlphaComponent(0.0)
        }) { (finish) in
            self.removeFromSuperview()
        }
    }
    
}
